using DongFangGuoKai.WMSPlatform.Commn.ExcelModule;
using DongFangGuoKai.WMSPlatform.Core;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System.Data;
using System.IO;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly WMSPlatformContext _context;


        public WeatherForecastController(ILogger<WeatherForecastController> logger, WMSPlatformContext context)
        {
            _logger = logger;

            _logger.LogError("111111");  
            _context = context;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpGet]
        [Route("DownloadExcel")]
        public IActionResult DownloadExcel()
        {

            var res = _context.TAreas.ToList();
            var memoryStream = ExcelImport.GetMemoryStream(res);

            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }
    }
}
